
//
//  ExpertBeFreeViewController.m
//  DaJiang
//
//  Created by sally on 16/10/6.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "AddFreeViewController.h"
#import "BeFreeCell.h"
#import "ExpertBeFreeViewController.h"
#import "ExpertModel.h"

@interface ExpertBeFreeViewController () <AddFreeVcDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;

@end

@implementation ExpertBeFreeViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
  [self loadData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"专家有空";
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
}

#pragma mark 获取数据
- (void)loadData {
  [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];

  //无翻页
  [FreeModel queryFreeListWithCompletion:^(NSMutableArray *freeList) {
    if (freeList) {
      [self.tableData removeAllObjects];
      [self.tableData addObjectsFromArray:freeList];
      [self.tableView reloadData];
    } else {
      NSError *error = [FreeModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  }];
}

#pragma mark - action
- (IBAction)actionOnAdd:(id)sender {
  AddFreeViewController *vc = [[AddFreeViewController alloc] init];
  vc.delegate = self;
  [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  BeFreeCell *cell = (BeFreeCell *) [tableView dequeueReusableCellWithIdentifier:@"BeFreeCell"];
  if (!cell) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"BeFreeCell" owner:nil options:nil] lastObject];
  }

  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  cell.cellData = [self.tableData objectAtIndex:indexPath.row];

  return cell;
}

#pragma mark AddFreeVcDelegate
- (void)refreshFreeListVc {
  [self loadData];
}

@end
